using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SMSSender.Entities.Models;
using SMSSender.Entities.Utils;
using SMSSender.Common;

namespace SMSSender.DAL.Base
{
	public class CustomerRoleDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CustomerRoleDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CustomerRole table.
		/// </summary>
		public virtual void Insert(CustomerRoleInfo customerRoleInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdRole", customerRoleInfo.IdRole),
				new SqlParameter("@IdPhone", customerRoleInfo.IdPhone),
				new SqlParameter("@RoleKey", customerRoleInfo.RoleKey),
				new SqlParameter("@RoleName", customerRoleInfo.RoleName),
				new SqlParameter("@Role", customerRoleInfo.Role)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerRole_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CustomerRole table.
		/// </summary>
		public virtual void Update(CustomerRoleInfo customerRoleInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdRole", customerRoleInfo.IdRole),
				new SqlParameter("@IdPhone", customerRoleInfo.IdPhone),
				new SqlParameter("@RoleKey", customerRoleInfo.RoleKey),
				new SqlParameter("@RoleName", customerRoleInfo.RoleName),
				new SqlParameter("@Role", customerRoleInfo.Role)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerRole_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CustomerRole table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idRole)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdRole", idRole)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CustomerRole_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CustomerRole table.
		/// </summary>
		public virtual CustomerRoleInfo Select(Guid? idRole)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdRole", idRole)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerRole_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCustomerRoleInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CustomerRole table.
		/// </summary>
		public List<CustomerRoleInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CustomerRole_SelectAll"))
			{
				List<CustomerRoleInfo> customerRoleInfoList = new List<CustomerRoleInfo>();
				while (dataReader.Read())
				{
					customerRoleInfoList.Add(MakeCustomerRoleInfo(dataReader));
				}

				return customerRoleInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CustomerRole class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CustomerRoleInfo MakeCustomerRoleInfo(SqlDataReader dataReader)
		{
			CustomerRoleInfo customerRoleInfo = new CustomerRoleInfo();
			customerRoleInfo.IdRole = SqlClientUtility.GetGuid(dataReader, DbConstants.CUSTOMERROLE.IDROLE);
			customerRoleInfo.IdPhone = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERROLE.IDPHONE);
			customerRoleInfo.RoleKey = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERROLE.ROLEKEY);
			customerRoleInfo.RoleName = SqlClientUtility.GetString(dataReader, DbConstants.CUSTOMERROLE.ROLENAME);
			customerRoleInfo.Role = SqlClientUtility.GetBoolean(dataReader, DbConstants.CUSTOMERROLE.ROLE);

			return customerRoleInfo;
		}

		#endregion
	}
}
